package com.flyco.modules.stm.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.flyco.modules.stm.entity.StmTransfer;
import com.flyco.modules.stm.entity.StmTransferItem;
import com.flyco.modules.stm.mapper.StmTransferItemMapper;
import com.flyco.modules.stm.mapper.StmTransferMapper;
import com.flyco.modules.stm.service.IStmTransferService;
import com.flyco.modules.stm.vo.StmPrintVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 门店调拨单
 * @Author: flyco
 * @Date:   2020-10-30
 * @Version: V1.0
 */
@DS("store")
@Service
public class StmTransferServiceImpl extends ServiceImpl<StmTransferMapper, StmTransfer> implements IStmTransferService {

	@Autowired
	private StmTransferMapper stmTransferMapper;
	@Autowired
	private StmTransferItemMapper stmTransferItemMapper;
	
	@Override
	@Transactional
	public void saveMain(StmTransfer stmTransfer, List<StmTransferItem> stmTransferItemList) {
		stmTransferMapper.insert(stmTransfer);
		for(StmTransferItem entity:stmTransferItemList) {
			//外键设置
			entity.setTransferId(stmTransfer.getId());
			stmTransferItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(StmTransfer stmTransfer,List<StmTransferItem> stmTransferItemList) {
		stmTransferMapper.updateById(stmTransfer);
		
		//1.先删除子表数据
		stmTransferItemMapper.deleteByMainId(stmTransfer.getId());
		
		//2.子表数据重新插入
		for(StmTransferItem entity:stmTransferItemList) {
			//外键设置
			entity.setTransferId(stmTransfer.getId());
			stmTransferItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		stmTransferItemMapper.deleteByMainId(id);
		stmTransferMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			stmTransferItemMapper.deleteByMainId(Long.valueOf(id.toString()));
			stmTransferMapper.deleteById(id);
		}
	}


	@Override
	public List<StmPrintVO> selectByIds(List<Long> ids, Date createTime) {
		return stmTransferItemMapper.selectByIds(ids, createTime);
	}
	
}
